"Colin's grep functionality vimrc

"Win32 vim defaults to findstr.exe instead of grep.  Use cygwin instead
if has("win32") " If we're running on windows
   set grepprg=grep\ -n
endif


"Add the way the errorlist window displays its information to the errorformat
"list.  This allows you to copy from the error list window and create a new
"errorlist file using that format (use the new errorlist file via :cfile)
set errorformat+=%f\|%l\|%m

"Functionality to quickly edit the error list manually.  Then, thanks
"to the addition to errorformat above, you can write the new version
"of the error list to a file and use :cfile to use it.
function! EditErrlist()
   botright copen
   1
   normal yG
   new
   normal pkdd
endfunction
command! EditErrlist call EditErrlist()

"TBD - something like this might be useful, but it needs work.
"The problem here is that it pops into the first error in the same
"buffer, which from EditErrlist is a small skinny little thing, and
"not in the main window (i.e. using :Wings) where the code should be.
"function! UseNewErrlist(filename)
"   execute "write" a:filename
"   execute "cfile" a:filename
"endfunction
"command! -nargs=1 UseNewErrlist call UseNewErrlist(<f-args>)

"Check for global flag that specifies whether or not I'm using CVS right now
if (exists("g:UsingCvs"))
   "Define the Grep command such that CVS/Base directories are filtered out
   "  This is highly useful since 'cvs edit' creates a copy of any file that is
   "  being edited.  So without this filter, CVS/Base files will frequently
   "  match searches.
   command! -nargs=* -complete=dir Grep :grep <args> \| grep -v CVS/Base
else
   "Define Grep command such that tag completion can be used
   "  (but mostly just so that behavior is similar to above, i.e. Grep command
   "  exists)
   command! -nargs=* -complete=dir Grep :grep <args>
endif

"
"Handy mappings to kick off grep
"
map ,g :Grep <cword>
"Recursive grep
map ,r :Grep -r <cword> .
map ,s :Grep -r --include=\*.{c,cpp,h} <cword> .


"
" STOP SOURCING THIS VIMRC FILE NOW
"
finish

" ==========================================================================
"                BELOW HERE IS FOR HISTORICAL PURPOSES ONLY
"                         (NOT CURRENTLY IN USE)
" ==========================================================================

"This function provides the ability to search recursively (from the cwd)
"for files of a certain type (e.g. *.{c,h} or just *.mk).
"
"note: Simple recursive searches can be done more simply by using:
"      'grep -r searchpattern .'
function! GrepFileTypesRecursive(grepargs, searchstring, searchfiletype)
   let l:searchpath = glob("`find . -type d`")
   if (strlen(l:searchpath) <= 0)
      echo "No files in search path. Did not execute grep."
      return
   endif
   "Replace dos backslashes with real slashes
   let l:searchpath = substitute(l:searchpath, "\\", "/", "g")
   "Add on the search file type regex that was passed in as an argument
   let l:searchpath = substitute(l:searchpath, "\n", "/" . a:searchfiletype . " ", "g")
   "The last line of output from the find doesn't seem to have a newline,
   "  so this will take care of it
   let l:searchpath = substitute(l:searchpath, "$",  "/" . a:searchfiletype, "g")
   "echo a:searchstring
   "echo l:searchpath
   execute "grep" a:grepargs a:searchstring l:searchpath
endfunction

"MyGrep command provides access to GrepFileTypesRecursive as follows:
"  usage example: MyGrep searchForFoo *.{c,h,mk}
command! -nargs=1 -complete=tag MyGrep call GrepFileTypesRecursive(" ",<f-args>)

"AMyGrep command provides access to GrepFileTypesRecursive as follows:
"  usage example: MyGrep -i searchForFoo *.{c,h,mk}
command! -nargs=1 -complete=tag AMyGrep call GrepFileTypesRecursive(<f-args>)

